package com.sky.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.constant.CacheConstant;
import com.sky.constant.PreConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.result.Result;
import com.sky.service.ICategoryService;
import com.sky.service.IDishFlavorService;
import com.sky.service.IDishService;
import com.sky.vo.DishVO;
import org.apache.poi.ss.usermodel.ConditionalFormattingThreshold;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.annotation.AccessType;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/admin/dish")
public class DishController {
    @Autowired
    private IDishService iDishService;
    @Autowired
    private ICategoryService iCategoryService;

    @Autowired
    private IDishFlavorService iDishFlavorService;


    @GetMapping("/page")
    public Result page(DishPageQueryDTO dishPageQueryDTO ){
        IPage<Dish> iPage =new Page<>(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        LambdaQueryWrapper<Dish> lqw1 =new LambdaQueryWrapper<>();
        lqw1.like(dishPageQueryDTO.getName()!=null&&!dishPageQueryDTO.getName().trim().isEmpty()
                ,Dish::getName,dishPageQueryDTO.getName());
        lqw1.eq(dishPageQueryDTO.getCategoryId()!=null,Dish::getCategoryId,dishPageQueryDTO.getCategoryId());
        lqw1.eq(dishPageQueryDTO.getStatus()!=null,Dish::getStatus,dishPageQueryDTO.getStatus());
//        lqw1.orderByDesc(Dish::getUpdateTime);
        iDishService.page(iPage,lqw1);

        List<Dish> records = iPage.getRecords();
        List<DishVO> voList =new ArrayList<>();
        for (Dish record : records) {
            LambdaQueryWrapper<Category> lqw =new LambdaQueryWrapper<>();
            lqw.eq(Category::getId,record.getCategoryId());
            Category one = iCategoryService.getOne(lqw);
            DishVO dishVo =new DishVO();
            BeanUtils.copyProperties(record,dishVo);
            dishVo.setCategoryName(one.getName());
            voList.add(dishVo);
        }
        IPage<DishVO> voIPage =new Page<>();
        voIPage.setRecords(voList);
        voIPage.setTotal(iPage.getTotal());
        return Result.success(voIPage);
    }

    @PostMapping
    @CacheEvict(cacheNames = CacheConstant.DISH_CACHE,key = "#dishDTO.categoryId")
    public Result addDish(@RequestBody  DishDTO dishDTO){
    //因为不是继承关系了，所以不能直接用多态，而是应该转换
        iDishService.addDishAndFlavor(dishDTO);
        return Result.success();
    }
    @PutMapping
    @CacheEvict(cacheNames = CacheConstant.DISH_CACHE,allEntries = true)
    public Result update(@RequestBody  DishDTO dishDTO){
        //但这里注解用的是删除全部的缓存，为啥不精确删除
        //原因为 如果我修改的是categoryId,那么别的也会受影响，所以要全部删除
        iDishService.updateDishAndFlavor(dishDTO);
        return Result.success();
    }

    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = CacheConstant.DISH_CACHE,allEntries = true)
    public Result status(@PathVariable  Integer status ,Long id){
        LambdaUpdateWrapper<Dish> luw =new LambdaUpdateWrapper<>();
        luw.set(Dish::getStatus,status).eq(Dish::getId,id);
        iDishService.update(luw);
        return Result.success();
    }
    @DeleteMapping
    @CacheEvict(cacheNames = CacheConstant.DISH_CACHE,allEntries = true)
    public Result delete (@RequestParam List<Long> ids){
        iDishService.removeByIds(ids);
        return Result.success("删除成功");
    }

    @GetMapping("/{id}")
    public Result getDish(@PathVariable Long id ){
        Dish byId = iDishService.getById(id);
        //前端不显示，要不就是数据不够，要不就是没查到
        DishDTO dishDTO = new DishDTO();
        BeanUtils.copyProperties(byId,dishDTO);
        //口味  根据dish_flavor 的 dishId查找f 的数组
        LambdaQueryWrapper<DishFlavor> lqw =new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId,id);
        List<DishFlavor> list = iDishFlavorService.list(lqw);
        dishDTO.setFlavors(list);
        //需要传一个Dto
        return Result.success(dishDTO);
    }

    @GetMapping("/list")
    public Result getList(Long categoryId){
        LambdaQueryWrapper<Dish> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Dish::getCategoryId,categoryId);
        List<Dish> list = iDishService.list(lqw);
        return Result.success(list);
    }
}
